/* ProxyBase.cs --
   Copyright (C) 2008 Pacific Biosciences, Inc. */


using System;

namespace SexpSharp.RPC
{
	public abstract class ProxyBase
	{
		protected RPCClient client;

		protected ProxyBase(RPCClient client)
		{
			if (client == null)
				throw new ArgumentNullException("client");
			this.client = client;
		}

		protected object InvokeRemote(string method, Type returnType, object[] args)
		{
			IValue[] arglist = new IValue[args.Length];
			for (int i = 0; i < args.Length; i++)
				arglist[i] = Values.ObjectToValue(args[i]);
			MethodCall call = new MethodCall(method, arglist);
			MethodFault fault = null;
			MethodResponse response = client.Execute(call, out fault);
			if (response != null)
			{
				if (returnType != null)
					return Values.ValueToObject(response.ReturnValue, returnType);
				else if (response.ReturnValue != null)
					throw new RPCException("got return value for void-returning method");
				else
					return null;
			}

			if (fault != null)
			{
				throw new Exception("FIXME -- bleah");
			}

			throw new RPCException("did not receive a response");
		}
	}
}
